package org.jdns.jfreechess.draw;

import org.jdns.jfreechess.ics.IcsMessageDispatcher;
import org.jdns.jfreechess.ics.IcsMessageReader;
import org.jdns.jfreechess.timeseal.TimesealSocket;
import org.noos.xing.mydoggy.ContentManager;
import org.noos.xing.mydoggy.ToolWindowAnchor;
import org.noos.xing.mydoggy.plaf.MyDoggyToolWindowManager;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JFreeChessMain {

  private final Logger log = Logger.getLogger(JFreeChessMain.class.getName());

  private final Action quitAction = new AbstractAction("Quit") {
    {
      putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('Q', InputEvent.CTRL_MASK));
      putValue(MNEMONIC_KEY, KeyEvent.VK_Q);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
      System.exit(0);
    }
  };

  static AtomicLong totalBytes = new AtomicLong(0);

  public static void main(String[] args) {
    new JFreeChessMain();
  }

  public JFreeChessMain() {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
    System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Test");

    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
      e.printStackTrace();
    }

    JFrame mainFrame = new JFrame("JFreeChess");
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File") {
      {
        setMnemonic('F');
      }
    };

    menuBar.add(fileMenu);
    fileMenu.add(quitAction);

    mainFrame.getContentPane().setLayout(new BorderLayout());

    MyDoggyToolWindowManager myDoggyToolWindowManager = new MyDoggyToolWindowManager();
    ContentManager contentManager = myDoggyToolWindowManager.getContentManager();

    mainFrame.setJMenuBar(menuBar);
    mainFrame.getContentPane().add(myDoggyToolWindowManager);
    mainFrame.pack();
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setVisible(true);

    myDoggyToolWindowManager.registerToolWindow("board1", "Board", null, new JChessBoard(),
        ToolWindowAnchor.TOP).setActive(true);

    TimesealSocket socket;
    try {
      socket = new TimesealSocket(InetAddress.getByName("freechess.org"), 5000);

      IcsMessageDispatcher dispatcher = new IcsMessageDispatcher(new IcsMessageReader(
          socket.getInputStream(), "fics% "));

      JConsole console = new JConsole(socket.getOutputStream());
      dispatcher.addIcsMessageHandler(console);
      dispatcher.start();
      dispatcher.addIcsMessageHandler(new Style12MessageHandler(contentManager));

      myDoggyToolWindowManager.registerToolWindow("Console", "Console", null, console,
          ToolWindowAnchor.BOTTOM).setActive(true);
    } catch (UnknownHostException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }
}
